package com.ruoyi.project.jiaqin.companyrecruit.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.companyrecruit.domain.CompanyRecruit;
import com.ruoyi.project.jiaqin.companyrecruit.mapper.CompanyRecruitMapper;
import com.ruoyi.project.jiaqin.companyrecruit.service.ICompanyRecruitService;
import com.ruoyi.project.jiaqin.recruitindustry.domain.RecruitIndustry;
import com.ruoyi.project.jiaqin.recruitindustry.service.IRecruitIndustryService;
import com.ruoyi.project.jiaqin.region.domain.Region;
import com.ruoyi.project.jiaqin.region.service.IRegionService;
import com.ruoyi.project.system.member.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 企业招聘Service业务层处理
 *
 * @author admin
 * @date 2021-01-13
 */
@Service
public class CompanyRecruitServiceImpl implements ICompanyRecruitService
{
    @Autowired
    private CompanyRecruitMapper companyRecruitMapper;
    @Autowired
    private IRegionService iRegionService;
    @Autowired
    private IRecruitIndustryService iRecruitIndustryService;
    @Autowired
    private ICompanyService iCompanyService;

    /**
     * 查询企业招聘
     *
     * @param id 企业招聘ID
     * @return 企业招聘
     */
    @Override
    public CompanyRecruit selectCompanyRecruitById(String id)
    {
        return companyRecruitMapper.selectCompanyRecruitById(id);
    }

    /**
     * 查询企业招聘列表
     *
     * @param companyRecruit 企业招聘
     * @return 企业招聘
     */
    @Override
    public List<CompanyRecruit> selectCompanyRecruitList(CompanyRecruit companyRecruit)
    {
        return companyRecruitMapper.selectCompanyRecruitList(companyRecruit);
    }

    @Override
    public JSONObject selectCompanyRecruitByIdA(String id) {
        JSONObject data = new JSONObject();
        CompanyRecruit companyRecruit = companyRecruitMapper.selectCompanyRecruitById(id);
        Company company = iCompanyService.selectCompanyById(companyRecruit.getCompanyId());
        RecruitIndustry recruitIndustry = iRecruitIndustryService.selectRecruitIndustryById(companyRecruit.getIndustry());
        Region province = iRegionService.selectRegionById(companyRecruit.getProvince());
        Region city = iRegionService.selectRegionById(companyRecruit.getCity());
        Region area = iRegionService.selectRegionById(companyRecruit.getArea());
        data.put("company",company);
        data.put("companyRecruit",companyRecruit);
        data.put("recruitIndustry",recruitIndustry);
        data.put("province",province);
        data.put("city",city);
        data.put("area",area);
        return data;
    }

    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IMemberService memberService;
    @Override
    public PageInfo<CompanyRecruit> selectCompanyRecruitPage(Integer pageNum,Integer pageSize,CompanyRecruit companyRecruit) {
        PageHelper.startPage(pageNum,pageSize);
        List<CompanyRecruit> list = companyRecruitMapper.selectCompanyRecruitList(companyRecruit);
        if(list != null && list.size()!=0 ){
            for (CompanyRecruit c:list) {
                c.setCompany(iCompanyService.selectCompanyById(c.getCompanyId()));
                Region provinceName = iRegionService.selectRegionById(c.getProvince());
                c.setProvinceName(provinceName.getName());
                Region cityName = iRegionService.selectRegionById(c.getCity());
                c.setCityName(cityName.getName());
                Company company = companyService.selectCompanyById(c.getCompanyId());
                if (company != null && company.getCompanyAvatar() != null) {
                    c.setHeadpic(company.getCompanyAvatar());
                } else {
                    c.setHeadpic("https://imgjq.szyujingkeji.com/2021/10/20/d394e8d5ef9d4fced9e9620b0d7ec545.jpg");
                }
            }
        }
        PageInfo<CompanyRecruit> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 新增企业招聘
     *
     * @param companyRecruit 企业招聘
     * @return 结果
     */
    @Override
    public int insertCompanyRecruit(CompanyRecruit companyRecruit) {
        companyRecruit.setId(UUID.randomUUID().toString().replace("-",""));
        companyRecruit.setCreateAt(new Date());
        return companyRecruitMapper.insertCompanyRecruit(companyRecruit);
    }

    /**
     * 修改企业招聘
     *
     * @param companyRecruit 企业招聘
     * @return 结果
     */
    @Override
    public int updateCompanyRecruit(CompanyRecruit companyRecruit) {
        return companyRecruitMapper.updateCompanyRecruit(companyRecruit);
    }

    /**
     * 删除企业招聘对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCompanyRecruitByIds(String ids)
    {
        return companyRecruitMapper.deleteCompanyRecruitByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除企业招聘信息
     *
     * @param id 企业招聘ID
     * @return 结果
     */
    @Override
    public int deleteCompanyRecruitById(String id)
    {
        return companyRecruitMapper.deleteCompanyRecruitById(id);
    }

}
